package com.seeyon.apps.schedule.manager.impl;

import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.ldmeeting.utils.LeavingUtil;
import com.seeyon.apps.schedule.manager.ScheduleManager;
import com.seeyon.apps.schedule.util.ScheduleUtil;
import com.seeyon.cap4.form.api.FormApi4Cap4;
import com.seeyon.cap4.form.bean.FormBean;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.cap4.form.bean.FormFieldBean;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.ctpenumnew.manager.EnumManager;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.common.po.ctpenumnew.CtpEnumBean;
import com.seeyon.ctp.common.po.ctpenumnew.CtpEnumItem;
import com.seeyon.ctp.util.FlipInfo;
import com.seeyon.ctp.util.Strings;
import com.seeyon.apps.schedule.dao.ScheduleDao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Title:ScheduleManagerImpl
 * Description: 盐城市政府项目-日程相关接口实现类
 * @author wxt.chenfan
 * @version 2024-11-28
 * @Date 2024-11-28
 */
public class ScheduleManagerImpl implements ScheduleManager {
    private FormApi4Cap4 formApi4Cap4;
    private ScheduleDao scheduleDao;

    public void setFormApi4Cap4(FormApi4Cap4 formApi4Cap4) {
        this.formApi4Cap4 = formApi4Cap4;
    }

    public void setScheduleDao(ScheduleDao scheduleDao) {
        this.scheduleDao = scheduleDao;
    }

    @Override
    public JSONObject getLeaveNum(JSONObject params) {
        JSONObject jsonObject = new JSONObject();
        //请假信息底表-市委底表表单编号
        String QJXXDB_SW = AppContext.getSystemProperty("schedule.QJXXDB_SW");
        if (Strings.isEmpty(QJXXDB_SW)) {
            jsonObject.put("code",400);
            jsonObject.put("mag","未配置表单模板编号");
            return jsonObject;
        }
        try {
            //请假信息底表-市委
            FormBean formBeanSW = formApi4Cap4.getFormByFormCode(QJXXDB_SW);
            String tableNameSW = formBeanSW.getMasterTableBean().getTableName();
            String leaveTimeSW = formBeanSW.getFieldBeanByDisplay(LeavingUtil.LEAVE_TIME_DISPLAY).getName();//离开时间
            String backTimeSW = formBeanSW.getFieldBeanByDisplay(LeavingUtil.BACK_TIME_DISPLAY).getName();//返回时间
            FormFieldBean fieldBeanByDisplay = formBeanSW.getFieldBeanByDisplay("处理状态-隐藏");
            long enumId = fieldBeanByDisplay.getEnumId();
            EnumManager enumManagerNew=(EnumManager) AppContext.getBean("enumManagerNew");
            CtpEnumBean anEnum = enumManagerNew.getEnum(enumId);
            Long itemId = null;
            for (CtpEnumItem item : anEnum.getItems()) {
                if ("已完成".equals(item.getShowvalue())){
                    itemId = item.getId();
                    break;
                }
            }
            String stateSW = fieldBeanByDisplay.getName();//状态
            StringBuffer sql = new StringBuffer();
            sql.append(" SELECT  COUNT(*) AS total_count FROM ");
            sql.append(tableNameSW).append(" WHERE 1=1 ");
            if (params.getString("date") == null || params.getString("date").equals("")) {
                sql.append("AND NOW() ");
            } else {
                sql.append("AND ");
                sql.append("'").append(params.getString("date")).append("'");
            }
            sql.append(" BETWEEN ").append(leaveTimeSW).append(" AND ").append(backTimeSW);
            sql.append(" and ").append(stateSW).append(" = '").append(itemId).append("'");
            List<Map<String, Object>> list = scheduleDao.getLeaveNum(String.valueOf(sql));
            if (list == null){
                jsonObject.put("code",400);
                jsonObject.put("mag","查询失败");
                return jsonObject;
            }
            Map<String, Object> map1 = list.get(0);
            jsonObject.put("code",200);
            jsonObject.put("msg","查询成功");
            jsonObject.put("data",map1.get("total_count"));
        } catch (BusinessException e) {
            ScheduleUtil.chenfan("获取请假人数失败","ScheduleManagerImpl.getLeaveNum",e);
        }
        return jsonObject;
    }

    @Override
    public FlipInfo getLeavedetails(FlipInfo flipInfo, Map<String, Object> params) {
        //请假信息底表-市委底表表单编号
        String QJXXDB_SW = AppContext.getSystemProperty("schedule.QJXXDB_SW");
        if (Strings.isEmpty(QJXXDB_SW)) {
            return null;
        }

        try {
            FormBean formBeanSW = formApi4Cap4.getFormByFormCode(QJXXDB_SW);
            String tableNameSW = formBeanSW.getMasterTableBean().getTableName();
            String nameSW = formBeanSW.getFieldBeanByDisplay(LeavingUtil.NAME_DISPLAY).getName();//姓名
            String outWhySW = formBeanSW.getFieldBeanByDisplay(LeavingUtil.OUT_WHY_DISPLAY).getName();//外出原因
            String leaveTimeSW = formBeanSW.getFieldBeanByDisplay(LeavingUtil.LEAVE_TIME_DISPLAY).getName();//离开时间
            String backTimeSW = formBeanSW.getFieldBeanByDisplay(LeavingUtil.BACK_TIME_DISPLAY).getName();//返回时间
            String postSW = formBeanSW.getFieldBeanByDisplay("职务").getName();//返回时间
            String leaveTimeXSSW = formBeanSW.getFieldBeanByDisplay("离开时间-显示").getName();//离开时间-显示
            String backTimeXSSW = formBeanSW.getFieldBeanByDisplay("返回时间-显示").getName();//返回时间-显示
            FormFieldBean fieldBeanByDisplay = formBeanSW.getFieldBeanByDisplay("处理状态-隐藏");
            long enumId = fieldBeanByDisplay.getEnumId();
            EnumManager enumManagerNew=(EnumManager) AppContext.getBean("enumManagerNew");
            CtpEnumBean anEnum = enumManagerNew.getEnum(enumId);
            Long itemId = null;
            for (CtpEnumItem item : anEnum.getItems()) {
                if ("已完成".equals(item.getShowvalue())){
                    itemId = item.getId();
                    break;
                }
            }
            String stateSW = fieldBeanByDisplay.getName();//状态
            StringBuffer sql = new StringBuffer();
            sql.append("  SELECT t2.").append(nameSW).append(" as name, t2.").append(postSW).append(" as post, t2.").append(outWhySW).append(" as cause, ");
            sql.append(leaveTimeXSSW).append(" as leaveTimeXSSW, ").append(backTimeXSSW).append(" as backTimeXSSW");
            sql.append(" FROM ").append(tableNameSW).append(" t2 ").append("WHERE 1=1 ");
            if (params.containsKey("date")){
                sql.append(" AND '").append(params.get("date")).append("' BETWEEN ").append(leaveTimeSW).append(" AND ").append(backTimeSW);
            } else {
                sql.append(" AND NOW() BETWEEN ").append(leaveTimeSW).append(" AND ").append(backTimeSW);
            }
            if (params != null && !params.isEmpty()){
                String key = String.valueOf(params.get("condition"));
                if (params.containsKey("condition") && Strings.isNotBlank(key)){
                    if ("name".equals(key)){
                        String value = String.valueOf(params.get("value"));
                        sql.append(" AND t2.").append(nameSW).append(" like '%").append(value).append("%' ");
                    }
                }
            }

            sql.append(" and ").append(stateSW).append(" = '").append(itemId).append("'");
            FlipInfo info = scheduleDao.getLeavedetails(flipInfo, String.valueOf(sql));
            List<Map<String, Object>> data = info.getData();
            List<Map<String, Object>> data1 = new ArrayList<>();
            for (Map<String, Object> row : data) {
                Map<String, Object> hashMap = new HashMap<>();
                hashMap.put("name",row.get("name"));
                hashMap.put("post",row.get("post"));
                hashMap.put("cause",row.get("cause"));
                hashMap.put("leaveDate",row.get("leavetimexssw"));
                hashMap.put("returnDate",row.get("backtimexssw"));
                data1.add(hashMap);
            }
            info.setData(data1);
            return info;
        } catch (Exception e){
            ScheduleUtil.chenfan("获取请假明细列表","ScheduleManagerImpl.getLeavedetails",e);
        }
        return null;
    }
}
